# ##############################################################################
# ####### Copyright (C) 2014 All Rights Reserved                        ########
# #######                                                               ########
# ####### Amy Dale                                                      ########
# ####### Carnegie Mellon University                                    ########
# #######                                                               ########
# ####### Homework Assignment 1, 19-704 (Applied Data Analysis)         ########
# ####### Date Created: 3-23-2014                                       ########
# #######                                                               ########
# ####### This code reviews some basic R operations                     ########
# ####### Inputs: none                                                  ########
# ####### Outputs: the sum of 2 and 2, a set of pre-defined             ########
# ####### vectors/matrices, and a histogram for a randomly              ########
# ####### sampled normal distribution                                   ########
# ##############################################################################

### Note: Functions precede the main body of the script
# ##############################################################################
DefineVars <- function(n = 8, verbose = TRUE) {
# Reviews some (very!) basic operations for defining/transforming vectors 
# and matrices
#
# Args:
#   x: highest integer in the sequence to be generated.  Default value is 8.
#   verbose: If TRUE, prints output. Default is TRUE.
# Returns: No object is returned
# No error handling used for this simple script ...
	
  y <- c(1, 2, 3, 4, 5, 6, 7, 8)
	
  if (verbose){
    cat("\n", sprintf("Consider three ways of defining a sequence 
					  from 1 to %d:",n))
    cat("\n\n", "Version 1:\n")
    print(y)
  }
	
  y <- seq(1, 8)
  if (verbose){
    cat("\n", "Version 2:\n")
	print(y)
  }
	
  y <- 1:8
  if (verbose){
    cat("\n", "Version 3:\n")
    print(y)
  }
	
# perform a simple transformation of y
# the shorter vector is recycled
  y.trans <- y / 2
  if (verbose){
    cat("\n", "All elements of y divided by 2:\n")
    print(y.trans)
  }
	
# Create a simple matrix of the values in y
# fill in the column by row rather than by the default (by column)
  y.matrix <- matrix(y, nrow=2, byrow=T)
  if (verbose){
    cat("\n", "Matrix of the elements in y:\n")
    print(y.matrix)
  }
}
# ##############################################################################


# ##############################################################################
ExampleDist <- function() {
# Sample 100 points randomly from a normal distribution and plot a histogram
# with an overlying density function
#
# Args: No arguments
# Returns: No object is returned
# No error handling used for this simple script ...
	
# Code from Dalgaard, Peter. Introductory Statistics with R. 2nd ed. 
# New York: Springer, 2008. Print.
#
# "freq(F)" specifies that densities should be used in place of absolute counts
# "add(T)" allows the curve to be overlaid on the existing plot
# To facilitate overlay, make sure that the range is large enough 
# to accomodate both the histogram and the curve
  x <- rnorm(100)
  h <- hist(x, plot=F)
  ylim <- range(0, h$density, dnorm(0))
  hist(x, freq=F, ylim=ylim)
  curve(dnorm(x), add=T)
}
# ##############################################################################



# "Main" body of the script
# ##############################################################################

test=2 + 2  # 1. Add 2 and 2

# 2. Print the results
# The benefit of the concatenation function cat() over print()
# is that it does not print quotation marks around the output
# Note that %d represents an integer and \n is the newline character
cat(sprintf("The sum of two and two is: %d", test), "\n") 


cat("\n\n", "Calling function DefineVars ...", "\n") 
DefineVars()

cat("\n\n", "Calling function ExampleDist ...", "\n") 
ExampleDist()

cat("\n\n", "Execution complete.  Hello, World!", "\n") 
# ##############################################################################


